package com.zdb.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdb.order.entity.*;
import com.zdb.order.mapper.OrderMasterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderMasterService {
    @Autowired
    private OrderMasterMapper orderMasterMapper;

    @Autowired
    private SellerInfoService sellerInfoService;

    @Autowired
    private BusinessService businessService;

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private OrderDetailService orderDetailService;
    /**
     * 根据用户id和订单类型查询所有订单
     * @param uid
     * @return
     */
    public List<OrderMaster> getListOrders(String uid, String orderType) {

        QueryWrapper<OrderMaster> wrapper = new QueryWrapper<>();
        OrderMaster orderMaster = new OrderMaster();
        if((Integer.parseInt(orderType))==0){       //未付款0
            wrapper.eq("uId", uid).eq("pay_status", Integer.parseInt(orderType));
        }else if(Integer.parseInt(orderType)==1){//配送中2,3
            wrapper.eq("uId", uid).in("pay_status", (Integer.parseInt(orderType)+1),(Integer.parseInt(orderType)+2));
        }else if(Integer.parseInt(orderType)==2){//已完成4
            wrapper.eq("uId", uid).eq("pay_status", (Integer.parseInt(orderType)+2));
        }

        List<OrderMaster> list = orderMasterMapper.selectList(wrapper);
        if(StringUtils.isEmpty(list)){
            return null;
        }

        List<OrderMaster> resultList = list.stream().map(item ->{
//            SellerInfo sellerInfo = sellerInfoService.getSellerInfoById(item.getsId());
            Business business = businessService.getSellerInfoById(item.getsId());

            if(!StringUtils.isEmpty(business)){
                item.setsName(business.getName());
            }

            String[] strIds = item.getpIds().split(",");
            List<OrderDetail> orderDetailList = orderDetailService.getOrderDetailByIds(item.getId(),strIds);
            if(!StringUtils.isEmpty(orderDetailList)){
                double orderPrice = 0.0;
                for(OrderDetail orderDetail :orderDetailList){
                    orderPrice += orderDetail.getProTotalPrice();
                }
                item.setOrderTotalPrice(orderPrice);
                item.setOdInfos(orderDetailList);
            }

            return item;
        }).collect(Collectors.toList());

        return resultList;
    }
    /**
     * 根据骑手id和订单类型查询所有订单
     * @param rid
     * @return
     */
    public List<OrderMaster> getListOrdersByRiders(String rid,String orderType){
        QueryWrapper<OrderMaster> wrapper = new QueryWrapper<>();
        OrderMaster orderMaster = new OrderMaster();
        System.out.println("orderType:"+(Integer.parseInt(orderType)+1));
        if(((Integer.parseInt(orderType)+1))==1){       //待接单
            System.out.println("进入接单");
            wrapper.eq("r_id", 0).eq("pay_status", 1);
        }else if((Integer.parseInt(orderType)+1)==2){//取单中
            System.out.println("进入取单");
            wrapper.eq("r_id", rid).eq("pay_status", (Integer.parseInt(orderType)+1));
        }else if((Integer.parseInt(orderType)+1)==3){//送单中
            System.out.println("进入送单");
            wrapper.eq("r_id", rid).eq("pay_status", (Integer.parseInt(orderType)+1));
        }else if((Integer.parseInt(orderType)+1)==4){//业绩统计
            System.out.println("此单完成");
            wrapper.eq("r_id", rid).eq("pay_status", (Integer.parseInt(orderType)+1));
        }
        List<OrderMaster> list = orderMasterMapper.selectList(wrapper);
        if(StringUtils.isEmpty(list)){
            return null;
        }

        List<OrderMaster> resultList = list.stream().map(item ->{
//            final SellerInfo sellerInfo = sellerInfoService.getSellerInfoById(item.getsId());
            Business business = businessService.getSellerInfoById(item.getsId());
            if(!StringUtils.isEmpty(business)){
                item.setsPhotoAddress(business.getsPhotoAddress());
            }
            return item;
        }).collect(Collectors.toList());
        return resultList;
    }

    /**
     * 根据骑手id，和订单id，进行接单
     * @param rid
     * @param orderId
     * @return
     */
    public int getClickOrder(String rid, String orderId) {
//        QueryWrapper<OrderMaster> wrapper = new QueryWrapper<>();

        OrderMaster orderMas = new OrderMaster();
        orderMas.setId(orderId);
        OrderMaster orderMaster = orderMasterMapper.selectOne(new QueryWrapper<OrderMaster>().eq("order_id", orderId));

        System.out.println("orderMaster：");
        System.out.println(orderMaster);

        int count = 0;
        if(!StringUtils.isEmpty(orderMaster)){
            orderMaster.setPayStatus("2");
            orderMaster.setrId(rid);
//            wrapper.setEntity(orderMas);

            count = orderMasterMapper.updateById(orderMaster);
        }
        return count;
    }
}
